var answerCells;
var cell;
var currentPage=0;
var input_currentPage=0;
var mouse_currentPage=0;
var rt_currentPage=0;
var cellNo;
var pageSize=10;

$(document).ready(function() {
	var url = window.location.href;
	var parastr = url.split("?");
	var answerIdstr = parastr[1].split("&");

	var answerId = answerIdstr[0].split("=")[1];
	var examId = answerIdstr[1].split("=")[1];

	answerCells = new Array();
	getAllAnswerCells(answerId, examId);
	
});

function getAllAnswerCells(answerId, examId) {
	$.ajax({
		url : "../answer/getAllAnswerCells.html",
		data : "answerId=" + answerId + "&examId=" + examId,
		type : 'post',
		dataType : 'json',
		contentType : "application/x-www-form-urlencoded;charset=utf-8",
		success : function(result) {
			if (result != null) {
				//第一张为项目简介
				for ( var i = 1; i < result.length; i++) {
					answerCells.push(result[i]);
				}
				cellNo = result.length;
				initData(currentPage);
				showRightRatio();
				
				$("#current-all-page").html(answerCells.length);
			} else {
				answerCells = null;
				alert("获取失败 请先登入");
			}
		},
		error : function() {
			answerCells = null;
			alert("连接服务器出错 或者  请重新登入");
		}
	});
}

/**
 * 显示该用户所提交的答卷的正确率<br>
 * 正确率 = (回答正确+无答案要求)/总<br>
 * 第一张为项目简介
 */
function showRightRatio(){
	var rightNum = 0;//正确的题数
	var unCareNum = 0;//无答案要求
	var wrongNum = 0;
	
	for(var i=0;i<answerCells.length;++i){
		var currCell = answerCells[i];
		var currAR = currCell.answerRight;
		if(currAR == -1){
			++unCareNum;
		}
		else if(currAR==1){
			++rightNum;
		}
		else{
			++wrongNum;
		}
	}
	
	var rightRatio = (rightNum+unCareNum)/answerCells.length>=1?"100%":(rightNum+unCareNum)/answerCells.length+"%";
	
	var str = "<br><font color='green' style='font-size:13px;'>正确 : </font>"+rightNum
	+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='red' style='font-size:13px;'>错误 </font> : "+wrongNum
	+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='#7fb80e' style='font-size:13px;'>无答案要求</font>: "+unCareNum
	+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color='blue' style='font-size:15px;'>正确率</font> : <font style='font-size:18px;'>"+rightRatio
	+"</font>";
	
	$("#rightRatio").html(str);
}


/**
 * 初始化页面上的信息
 */
function initData(no) {
	//初始化用户输入信息
	input_currentPage=0;
	mouse_currentPage=0;
	rt_currentPage=0;
	
	
	cell = answerCells[no];

	// 答案
	var answerRight = cell.answerRight;
	if (answerRight == -1) {
		$("#answerRight").html("本张无答案要求");
	} else if (answerRight == 0) {
		$("#answerRight").html("<font color='red'>错误</font>");
	} else {
		$("#answerRight").html("<font color='green'>正确</font>");
	}

	// 鼠标移动信息
	showMouseAction();
	//用户输入信息
	showInputAction();
	//反应时间
	showRT();
	
}

/**
 * 下一张PPT的答案
 */
$(".next-answer").click(function(){
	currentPage++;
	
	if(currentPage+1>=cellNo){
		$(this).attr("disabled",true);
	}
	initData(currentPage);
	$("#current-page").html(currentPage+1);
	$(".prev-answer").attr("disabled",false);
});

/**
 * 上一张PPT的答案
 */
$(".prev-answer").click(function(){
	currentPage--;
	
	if(currentPage==0){
		$(this).attr("disabled",true);
	}
	initData(currentPage);
	$("#current-page").html(currentPage+1);
	$(".next-answer").attr("disabled",false);
});


/**
 * 鼠标移动信息
 */
function showMouseAction(){
	var mouseAction = cell.mouseAction;
	if (mouseAction == undefined || mouseAction.trim() == "") {
		$("#mouseAction").html("本张无记录要求");
		$("#mouseOperate").css("display","none");
	} else {
		//建表
		$("#mouseOperate").css("display","block");
		$("#mouseAction").html("<table id='mousedata' cellpadding='0' cellspacing='0'></table>");
		var table = $("#mousedata");
		table.append("<tr class='table-th'><td>次数</td><td>事件</td><td>X</td><td>Y</td><td>时间</td></tr>");
		
		var mss = mouseAction.split(",");
		var length = mss.length;
		var pages;
		if(length%pageSize==0){
			pages = parseInt(length/pageSize);//总共页数
		}
		else{
			pages = parseInt(length/pageSize)+1;//总共页数
		}
		if(mouse_currentPage<=0){
			mouse_currentPage = 0;
			$("#mouse-prev").attr("disabled",true);
			if(pages>1){
				$("#mouse-next").attr("disabled",false);
			}
		}
		else if(mouse_currentPage+1>=pages){
			mouse_currentPage = pages-1;//最后一页
			$("#mouse-next").attr("disabled",true);
			if(pages>1){
				$("#mouse-prev").attr("disabled",false);
			}
		}
		
		var start = mouse_currentPage*pageSize;
		var end = (mouse_currentPage+1)*pageSize>length?length:(mouse_currentPage+1)*pageSize;
		
		for ( var j = start; j < end; j++) {
			var ms = mss[j];
			var m = ms.split("-");
			// 类型
			var type = m[3];
			var html="";
			if (type == 0) {
				html = "<font color='8B7300'>移动</font>";
			} else if (type == 1) {
				html = "<font color='#FF3F3F'>左击</font>";
			} else if (type == 2) {
				html = "<font color='blue'>中击</font>";
			} else {
				html = "<font color='green'>右击</font>";
			}
			
			if(m[2]<=500){
				table.append("<tr><td>"+j+"</td><td>"+html+"</td><td>"+parseInt(m[0])+"</td><td>"+m[1]+"</td><td><font color='green'>"+m[2]+"</font></td></tr>");
			}
			else if(m[2]<=1000){
				table.append("<tr><td>"+j+"</td><td>"+html+"</td><td>"+parseInt(m[0])+"</td><td>"+m[1]+"</td><td><font color='#008B73'>"+m[2]+"</font></td></tr>");
			}
			else if(m[2]<=2000){
				table.append("<tr><td>"+j+"</td><td>"+html+"</td><td>"+parseInt(m[0])+"</td><td>"+m[1]+"</td><td><font color='#0053AB'>"+m[2]+"</font></td></tr>");
			}
			else if(m[2]<=5000){
				table.append("<tr><td>"+j+"</td><td>"+html+"</td><td>"+parseInt(m[0])+"</td><td>"+m[1]+"</td><td><font color='#C33B00'>"+m[2]+"</font></td></tr>");
			}
			else{
				table.append("<tr><td>"+j+"</td><td>"+html+"</td><td>"+parseInt(m[0])+"</td><td>"+m[1]+"</td><td><font color='red'>"+m[2]+"</font></td></tr>");
			}
			
		}
		
		if(pages<=1){
			$("#mouse-prev").attr("disabled",true);
			$("#mouse-next").attr("disabled",true);
			$("#mouse-go").attr("disabled",true);
			$("#mouse-submit").attr("disabled",true);
		}
		else{
			$("#mouse-prev").attr("disabled",false);
			$("#mouse-next").attr("disabled",false);
			$("#mouse-go").attr("disabled",false);
			$("#mouse-submit").attr("disabled",false);
		}
		
		$("#mouse-go").val(mouse_currentPage+1);
		$("#mouse-total-page").html(pages);
	}
}
$("#mouse-prev").click(function(){
	mouse_currentPage--;
	showMouseAction();
});
$("#mouse-next").click(function(){
	mouse_currentPage++;
	showMouseAction();
});
$("#mouse-submit").click(function(){
	var val = $("#mouse-go").val();
	if(isNaN(val)){
		val = 0;
	}
	mouse_currentPage=parseInt(val)-1;
	showMouseAction();
});




/**
 * 用户输入信息
 */
function showInputAction(){
	var inputAction = cell.inputAction;
	if(inputAction==undefined || inputAction.trim()==""){
		$("#inputAction").html("无");
		$("#inputOperate").css("display","none");
	}
	else{
		$("#inputOperate").css("display","block");
		$("#inputAction").html("<table id='inputdata' cellpadding='0' cellspacing='0' ></table>");
		var table = $("#inputdata");
		table.append("<tr class='table-th'><td>次数</td><td>输入</td><td>时间</td></tr>");
		
		var inputs = inputAction.split(","); 
		var length = inputs.length;
		var pages;
		if(length%pageSize==0){
			pages = parseInt(length/pageSize);//总共页数
		}
		else{
			pages = parseInt(length/pageSize)+1;//总共页数
		}
		
		if(input_currentPage<=0){
			input_currentPage = 0;
			$("#input-prev").attr("disabled",true);
			if(pages>1){
				$("#input-next").attr("disabled",false);
			}
		}
		else if(input_currentPage+1>=pages){
			input_currentPage = pages-1;//最后一页
			$("#input-next").attr("disabled",true);
			if(pages>1){
				$("#input-prev").attr("disabled",false);
			}
		}
		
		var start = input_currentPage*pageSize;
		var end = (input_currentPage+1)*pageSize>length?length:(input_currentPage+1)*pageSize;
		
		
		for(var j=start;j<end;j++){
			var input = inputs[j].split("-");
			
			if(input[1]<=500){
				table.append("<tr><td>"+j+"</td><td>"+input[0]+"</td><td><font color='green'>"+input[1]+"</font></td></tr>");
			}
			else if(input[1]<=1000){
				table.append("<tr><td>"+j+"</td><td>"+input[0]+"</td><td><font color='#008B73'>"+input[1]+"</font></td></tr>");
			}
			else if(input[1]<=2000){
				table.append("<tr><td>"+j+"</td><td>"+input[0]+"</td><td><font color='#0053AB'>"+input[1]+"</font></td></tr>");
			}
			else if(input[1]<=5000){
				table.append("<tr><td>"+j+"</td><td>"+input[0]+"</td><td><font color='#C33B00'>"+input[1]+"</font></td></tr>");
			}
			else{
				table.append("<tr><td>"+j+"</td><td>"+input[0]+"</td><td><font color='red'>"+input[1]+"</font></td></tr>");
			}
		}
		
		if(pages<=1){
			$("#input-prev").attr("disabled",true);
			$("#input-next").attr("disabled",true);
			$("#input-go").attr("disabled",true);
			$("#input-submit").attr("disabled",true);
		}
		else{
			$("#input-prev").attr("disabled",false);
			$("#input-next").attr("disabled",false);
			$("#input-go").attr("disabled",false);
			$("#input-submit").attr("disabled",false);
		}
		
		
		$("#input-go").val(input_currentPage+1);
		$("#input-total-page").html(pages);
	}
}

//用户信息上页
$("#input-prev").click(function(){
	input_currentPage--;
	showInputAction();
});
$("#input-next").click(function(){
	input_currentPage++;
	showInputAction();
});
$("#input-submit").click(function(){
	var val = $("#input-go").val();
	if(isNaN(val)){
		val = 0;
	}
	input_currentPage=parseInt(val)-1;
	showInputAction();
});



//反应时间信息

function showRT(){
	
	var rt = cell.rt;
	if(rt==undefined || rt.trim()==""){
		$("#rt").html("无");
		$("#rtOperate").css("display","none");
	}
	else if(rt=="0"){
		$("#rt").html("本张无记录要求");
		$("#rtOperate").css("display","none");
	}
	else{
		$("#rtOperate").css("display","block");
		$("#rt").html("<table id='rtdata' cellpadding='0' cellspacing='0' ></table>");
		var table = $("#rtdata");
		table.append("<tr class='table-th'><td width='199px'>次数</td><td>反应时间</td></tr>");
		
		var rts = rt.split("-"); 
		var length=rts.length;
		var pages;
		
		if(length%pageSize==0){
			pages = parseInt(length/pageSize);//总共页数
		}
		else{
			pages = parseInt(length/pageSize)+1;//总共页数
		}
		if(rt_currentPage<=0){
			rt_currentPage = 0;
			$("#rt-prev").attr("disabled",true);
			if(pages>1){
				$("#rt-next").attr("disabled",false);
			}
		}
		else if(rt_currentPage+1>=pages){
			rt_currentPage = pages-1;//最后一页
			$("#rt-next").attr("disabled",true);
			if(pages>1){
				$("#rt-prev").attr("disabled",false);
			}
		}
		
		var start = rt_currentPage*pageSize;
		var end = (rt_currentPage+1)*pageSize>length?length:(rt_currentPage+1)*pageSize;
		
		for(var j=start;j<end;j++){
			if(rts[j]<=500){
				table.append("<tr><td>"+j+"</td><td><font color='green'>"+rts[j]+"</font></td></tr>");
			}
			else if(rts[j]<=1000){
				table.append("<tr><td>"+j+"</td><td><font color='#008B73'>"+rts[j]+"</font></td></tr>");
			}
			else if(rts[j]<=2000){
				table.append("<tr><td>"+j+"</td><td><font color='#0053AB'>"+rts[j]+"</font></td></tr>");
			}
			else if(rts[j]<=5000){
				table.append("<tr><td>"+j+"</td><td><font color='#C33B00'>"+rts[j]+"</font></td></tr>");
			}
			else{
				table.append("<tr><td>"+j+"</td><td><font color='red'>"+rts[j]+"</font></td></tr>");
			}
		}
		
		if(pages<=1){
			$("#rt-prev").attr("disabled",true);
			$("#rt-next").attr("disabled",true);
			$("#rt-go").attr("disabled",true);
			$("#rt-submit").attr("disabled",true);
		}
		else{
			$("#rt-prev").attr("disabled",false);
			$("#rt-next").attr("disabled",false);
			$("#rt-go").attr("disabled",false);
			$("#rt-submit").attr("disabled",false);
		}
		
		$("#rt-go").val(rt_currentPage+1);
		$("#rt-total-page").html(pages);
	}
}


$("#rt-prev").click(function(){
	rt_currentPage--;
	showRT();
});
$("#rt-next").click(function(){
	rt_currentPage++;
	showRT();
});
$("#rt-submit").click(function(){
	var val = $("#rt-go").val();
	if(isNaN(val)){
		val = 0;
	}
	rt_currentPage=parseInt(val)-1;
	showRT();
});